<template>
  <div class="storage-manage">
    <div class="page-header">
      <h2>存储源管理</h2>
      <el-button type="primary" :icon="Plus" @click="handleAdd">添加存储源</el-button>
    </div>
    
    <el-table :data="storageList" v-loading="loading" style="width: 100%">
      <el-table-column prop="name" label="名称" width="200" />
      <el-table-column prop="key" label="别名" width="150" />
      <el-table-column prop="type" label="类型" width="150">
        <template #default="{ row }">
          {{ getStorageTypeName(row.type) }}
        </template>
      </el-table-column>
      <el-table-column prop="enable" label="状态" width="100">
        <template #default="{ row }">
          <el-tag :type="row.enable ? 'success' : 'info'">
            {{ row.enable ? '启用' : '停用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="orderNum" label="排序" width="100" />
      <el-table-column label="操作" width="300" fixed="right">
        <template #default="{ row }">
          <el-button type="text" :icon="Edit" @click="handleEdit(row)">编辑</el-button>
          <el-button 
            type="text" 
            :icon="row.enable ? 'VideoPause' : 'VideoPlay'"
            @click="handleToggleEnable(row)"
          >
            {{ row.enable ? '停用' : '启用' }}
          </el-button>
          <el-button type="text" :icon="Delete" style="color: #f56c6c" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus, Edit, Delete } from '@element-plus/icons-vue'
import { adminApi } from '@/api'

const router = useRouter()
const storageList = ref([])
const loading = ref(false)

onMounted(async () => {
  await loadStorageList()
})

const loadStorageList = async () => {
  loading.value = true
  try {
    const res = await adminApi.getStorageList()
    storageList.value = res.data || []
  } catch (error) {
    ElMessage.error('加载存储源列表失败')
  } finally {
    loading.value = false
  }
}

const getStorageTypeName = (type) => {
  const typeMap = {
    'local': '本地存储',
    'aliyun': '阿里云 OSS',
    'tencent': '腾讯云 COS',
    'qiniu': '七牛云',
    's3': 'S3',
    'onedrive': 'OneDrive',
    'google-drive': 'Google Drive',
    'ftp': 'FTP',
    'sftp': 'SFTP'
  }
  return typeMap[type] || type
}

const handleAdd = () => {
  router.push('/admin/storage/edit')
}

const handleEdit = (row) => {
  router.push(`/admin/storage/edit/${row.id}`)
}

const handleToggleEnable = async (row) => {
  try {
    if (row.enable) {
      await adminApi.disableStorage(row.id)
      ElMessage.success('已停用')
    } else {
      await adminApi.enableStorage(row.id)
      ElMessage.success('已启用')
    }
    await loadStorageList()
  } catch (error) {
    ElMessage.error('操作失败')
  }
}

const handleDelete = async (row) => {
  try {
    await ElMessageBox.confirm(`确定要删除存储源 "${row.name}" 吗？`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    await adminApi.deleteStorage(row.id)
    ElMessage.success('删除成功')
    await loadStorageList()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}
</script>

<style lang="scss" scoped>
.storage-manage {
  .page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    
    h2 {
      margin: 0;
      font-size: 20px;
      font-weight: 500;
    }
  }
}
</style>

